package com.zqweb.gulimall.coupon.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.zqweb.common.to.MemberPrice;
import com.zqweb.common.to.SkuFullReductionTo;
import com.zqweb.gulimall.coupon.entity.MemberPriceEntity;
import com.zqweb.gulimall.coupon.entity.SkuLadderEntity;
import com.zqweb.gulimall.coupon.service.MemberPriceService;
import com.zqweb.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.coupon.entity.SkuFullReductionEntity;
import com.zqweb.gulimall.coupon.service.SkuFullReductionService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 商品满减信息
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 20:22:01
 */
@RestController
@RequestMapping("coupon/skufullreduction")
public class SkuFullReductionController {
    @Autowired
    private SkuFullReductionService skuFullReductionService;
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @PostMapping("/saveInfo")
    public R saveInfo(@RequestBody SkuFullReductionTo skuFullReductionTo){
        Long skuId = skuFullReductionTo.getSkuId();
        // 1、sms_sku_ladder
        // 打折折扣 > 0 才作保存
        if(skuFullReductionTo.getFullCount() > 0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuFullReductionTo,skuLadderEntity);
            skuLadderEntity.setAddOther(skuFullReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }


        // 2、sms_sku_full_reduction
        // 减免价格 > 0 也才作保存
        if(skuFullReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuFullReductionTo,skuFullReductionEntity);
            skuFullReductionService.save(skuFullReductionEntity);
        }

        // 3、sms_member_price
        List<MemberPriceEntity> memberPriceEntities = skuFullReductionTo.getMemberPrice().stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuId);
            memberPriceEntity.setAddOther(1);
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            return memberPriceEntity;
            // 对于会员优惠价格不大于0的记录，都进行剔除，不作保存
        }).filter(member -> member.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuFullReductionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SkuFullReductionEntity skuFullReduction = skuFullReductionService.getById(id);

        return R.ok().put("skuFullReduction", skuFullReduction);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuFullReductionEntity skuFullReduction){
		skuFullReductionService.save(skuFullReduction);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuFullReductionEntity skuFullReduction){
		skuFullReductionService.updateById(skuFullReduction);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		skuFullReductionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
